yjx.controller('clientListController',['$rootScope','$scope','$timeout','$fetch','modalService','$timeout','$state',function ($rootScope,$scope,$timeout,$fetch,modalService,$timeout,$state) {
    $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[3];
    var clientListApi='/customer/getList';
    var deleteMsg = "是否删除该用户信息";
    $scope.addTag = false;
    $scope.showEdit = false;
    $scope.showPdfTag = false;
    var lastPdfUrl = '';
    $scope.pdfLoading =true;
    /*获取客户列表*/
    function getClientList() {
        $fetch.fetch(
            clientListApi,
            {
                params:{
                    userId:$rootScope.userId
                },
                cache:false,
            }
        ).then(function (data) {
                $timeout(function(){
                    if(data) {
                        $scope.clientList = data
                    }else{
                        $scope.clientList = [];
                    }
                })
            }).catch(function (e) {

            })
    }
    getClientList();
    /*删除客户*/
    var deleteId = null;
    $scope.deleteClient = function (id) {
        deleteId = id;
        modalService.confirm(deleteMsg,confirm)
    }
    function confirm() {
        $fetch.fetch(
            '/customer/remove',
            {
                params:{
                    userId:$rootScope.userId,
                    id:deleteId
                },
                type:'POST'
            }
        ).then(function(data){
                $timeout(function(){
                    for(var i in $scope.clientList){
                        if(deleteId == $scope.clientList[i].id) {
                            $scope.clientList.splice(i,1)
                            deleteId = null;
                        }
                    }
                })
            })
    }
    /*预约客户*/
    var reservationMsg = "该用户已预约成功！<br>您将在待办事项中收到提醒！"
    $scope.reservation = function (id) {
        /*调接口*/
        $fetch.fetch(
            '/customer/bespeak',
            {
                params:{
                    userId:$rootScope.userId,
                    customerIds:id,
                    type:1
                },
                cache:false

            }
        ).then(function(data){
                modalService.mainMsgInfo("待办事项中将出现这些提醒",'','预约成功')
            }).catch(function(e){
                modalService.mainMsgError(e.msg);
            })
    }
    /*编辑客户信息*/
    $scope.shared = {
        showEdit:false,
        isEdit:false
    }

    $scope.editInfo = function (item) {
        $scope.btnValue = '确认修改';
        //console.log("dkkklll;")
        $scope.shared.showEdit = true;
        $scope.shared.isEdit = true;
        $scope.editClient = item;
    }
    /*新增客户信息*/
    $scope.addInfo = function () {
        $scope.btnValue = '确认新增';
        $state.go('clientList.info');
        $scope.shared.showEdit = true;
        $scope.shared.isEdit = false;
        $scope.editClient = {};
    }
    /*预览PDF*/
    $scope.onePageShow = false;
    $scope.showPdfAct = function(item){
        $scope.sendEmailItem = item;
        var pdfUrl = item.pdfUrl;
        if(!pdfUrl){
            return
        }
        if(lastPdfUrl != '' && pdfUrl != lastPdfUrl ) {
            /*清除之前的canvas*/
            var canvasp = document.getElementById('pdf-container');
            canvasp.style.display = "none";
            angular.element(document.querySelectorAll('.pagesCanvas')).detach();
        }
        lastPdfUrl = pdfUrl
        //console.log(pdfUrl);
        //getBinaryData(pdfUrl);
        showPdf(pdfUrl)
    }

    function showPdf(pdfUrl) {

        var url = pdfUrl;
        //var url = 'helloworld.pdf';
        $scope.showPdfTag =true;
        $scope.pdfLoading =true;
        PDFJS.workerSrc = 'common/libs/pdf.worker.js';

        PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
            var pdfmodal = document.getElementById('pdfModal');
            //console.log(pdf.numPages);
            /*当pdf只有一页时 使用循环页面的方式加载不出来 原因未知*/
            if(pdf.numPages>2){
                var canvasp = document.getElementById('pdf-container');
                //canvas.style({display:block})
                canvasp.style.display = "none";
                for (var i = 1; i < pdf.numPages; i++) {

                    pdf.getPage(i).then(function getPageHelloWorld(page) {
                        //console.log(page);
                        var scale = 0.6;
                        var viewport = page.getViewport(scale);
                        var canvas = document.createElement('canvas');
                        var context = canvas.getContext('2d');
                        canvas.className = 'pagesCanvas';
                        canvas.height = viewport.height;
                        canvas.width = viewport.width;

                        // 这里拿body当pdf容器
                        pdfmodal.appendChild(canvas);
                        //
                        // Render PDF page into canvas context
                        //
                        $timeout(function(){
                            page.render({canvasContext: context, viewport: viewport});
                            $scope.pdfLoading = false;
                        })
                    });
                }
            }else{
                PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
                    pdf.getPage(1).then(function getPageHelloWorld(page) {
                        var scale = 0.6;
                        var viewport = page.getViewport(scale);
                        var canvas = document.getElementById('pdf-container');
                        //canvas.style({display:block})
                        canvas.style.display = "inline-block";
                        var context = canvas.getContext('2d');
                        canvas.height = viewport.height;
                        canvas.width = viewport.width;
                        var renderContext = {
                            canvasContext: context,
                            viewport: viewport
                        };
                        $timeout(function(){
                            page.render(renderContext);
                            $scope.pdfLoading = false;
                        })
                    });
                });
            }
        });
    }

    //function callGetDocment (response) {
    //    // body...
    //
    //    PDFJS.getDocument(response).then(function getPdfHelloWorld(_pdfDoc) {
    //        pdfDoc = _pdfDoc;
    //        renderPage(pageNum);
    //    });
    //}


    /*pdf二进制文件读取*/
    //function getBinaryData (url) {
    //    // body...
    //    var xhr = new XMLHttpRequest();
    //
    //    xhr.open('GET', url, true);
    //    xhr.responseType = 'arraybuffer';
    //
    //    xhr.onload = function(e) {
    //        //binary form of ajax response,
    //        showPdf(e.currentTarget.response);
    //    };
    //
    //    xhr.onerror = function  (res) {
    //        // body...
    //        alert(res);
    //    }
    //
    //    xhr.send();
    //}

    /*发送邮件*/
    $scope.sendEmail = function(item){
        if(!item.email){
            modalService.mainMsgInfo("","","当前客户没有邮箱,请先补全资料");
            return;
        }
        $fetch.fetch(
            '/testreport/sendmail',
            {
                params:{
                    customerEmail:item.email,
                    testreportId:item.testreportId,
                    userId:$rootScope.userId,
                    customerId:item.id,
                },
                cache:false,
                type:'post'
            }
        ).then(function(data){
                $timeout(function(){
                    $scope.showPdfTag = false;
                    modalService.mainMsgInfo("","","检测报告已成功发送至TA的邮箱");
                })
            }).catch(function(e){
                    $scope.showPdfTag = false;
                    modalService.mainMsgError(e.msg);
            })
    }
}])